package com.yellowpage.nsp.dao;

import java.io.FileInputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.xmlbeans.impl.jam.mutable.MPackage;
import org.hibernate.metamodel.relational.Size;

import com.mysql.jdbc.PreparedStatement;
import com.yellowpage.nsp.data.NspLogger;
import com.yellowpage.nsp.data.NspTelLog;
import com.yellowpage.white.util.SqlUtil;

public class RemoteNspDAO {

	private final static Properties properties = new Properties();
	private final static Logger logger = Logger.getLogger(RemoteNspDAO.class);

	String nsp_db_driver;
	String nsp_db_url;
	String nsp_db_username;
	String nsp_db_password;

	public RemoteNspDAO() {
		try {
			// properties.load(new FileInputStream("jdbc.properties"));

			properties.load(this.getClass().getClassLoader()
					.getResourceAsStream("jdbc.properties"));

		} catch (Exception e) {
			e.printStackTrace();
		}

		nsp_db_driver = properties.getProperty("nsp_db_driver");
		nsp_db_url = properties.getProperty("nsp_db_url");
		nsp_db_username = properties.getProperty("nsp_db_username");
		nsp_db_password = properties.getProperty("nsp_db_password");

		/*
		 * nsp_db_driver="com.mysql.jdbc.Driver"; nsp_db_url=
		 * "jdbc:mysql://192.168.28.176:3306/yp?userUnicode=true&amp;characterEncoding=utf8"
		 * ; nsp_db_username="root"; nsp_db_password="114omc";
		 */

	}

	public List getNspLogger(String startId) {

		List loggerList = new ArrayList();

		try {

			SqlUtil bach = SqlUtil.newInstance();
			bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
					nsp_db_password);

			String sql_connect = "select id, logid, requestip, requesttype, requestinfo, requesttime,"
					+ "       responsecode, responsedata, responseinfo "
					+ "  from logger where id > " + startId;

			System.out.println("Query:" + sql_connect);

			List rst = bach.executeQuery(sql_connect, null);

			for (int i = 0; i < rst.size(); i++) {
				Object[] o = (Object[]) rst.get(i);

				NspLogger nspLogger = new NspLogger();

				nspLogger.setId(((Integer) o[0]).intValue());
				nspLogger.setLogId((String) o[1]);
				nspLogger.setRequestIp((String) o[2]);
				nspLogger.setRequestType((String) o[3]);
				nspLogger.setRequestInfo((String) o[4]);
				nspLogger.setRequestTime((String) o[5]);
				nspLogger.setResponseCode((String) o[6]);
				nspLogger.setResponseData((String) o[7]);
				nspLogger.setResponseInfo((String) o[8]);

				loggerList.add(nspLogger);
			}

		} catch (Exception e) {
			logger.info("NSP get Data Error! Exception :" + e);
			e.printStackTrace();
		}

		return loggerList;

	}

	public List getNspTellog(String startId) {

		List tellogList = new ArrayList();

		try {

			SqlUtil bach = SqlUtil.newInstance();
			bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
					nsp_db_password);

			String sql_connect = "select id, type, queryNum, timestamp, sign, subjectNum "
					+ "  from tellog where id > " + startId;

			System.out.println("Query:" + sql_connect);

			List rst = bach.executeQuery(sql_connect, null);

			for (int i = 0; i < rst.size(); i++) {
				Object[] o = (Object[]) rst.get(i);

				NspTelLog nspTelLog = new NspTelLog();

				nspTelLog.setId(((Integer) o[0]).intValue());
				nspTelLog.setTelType((String) o[1]);
				nspTelLog.setQueryNum(o[2] != null ? new String((byte[]) o[2])
						: null);
				nspTelLog.setLogTime((String) o[3]);
				nspTelLog.setSignNote((String) o[4]);
				nspTelLog.setSubjectNum((String) o[5]);

				tellogList.add(nspTelLog);
			}

		} catch (Exception e) {
			logger.info("NSP get Data Error! Exception :" + e);
			e.printStackTrace();
		}

		return tellogList;

	}

	// 1
	public List getNspPartner() {

		List<Map<String, String>> partnerObj = new ArrayList<Map<String, String>>();
		// Object[] partnerObj=null;
		try {

			SqlUtil bach = SqlUtil.newInstance();
			bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
					nsp_db_password);

			String sql_connect = "select PARTNER_ID,LAST_UPDATE_TIME from omc_partner where 1=1";

			List<Object[]> rst = bach.executeQuery(sql_connect, null);
			if (rst.size() > 0) {
				for (Object[] obj : rst) {
					Map<String, String> map = new HashMap<String, String>();

					map.put("PARTNER_ID", obj[0] + "");
					if (obj[1] == null) {
						map.put("LAST_UPDATE_TIME", "");
					} else {
						map.put("LAST_UPDATE_TIME", obj[1] + "");
					}

					partnerObj.add(map);
				}

			}

		} catch (Exception e) {
			logger.info("NSP get Data Error! Exception :" + e);
			e.printStackTrace();
		}

		return partnerObj;
	}

	// 2
	public List getNspLastUpdatePartnerInterface(String id) {

		List<Map<String, String>> partnerObj = new ArrayList<Map<String, String>>();
		// Object[] partnerObj=null;
		try {

			SqlUtil bach = SqlUtil.newInstance();
			bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
					nsp_db_password);

			String sql_connect = "select PARTNER_INTERFACE_ID,LAST_UPDATE_TIME from omc_partner_interface where 1=1 AND PARTNER_INTERFACE_ID='"+id+"'";

			List<Object[]> rst = bach.executeQuery(sql_connect, null);
			if (rst.size() > 0) {
				for (Object[] obj : rst) {
					Map<String, String> map = new HashMap<String, String>();

					map.put("PARTNER_INTERFACE_ID", obj[0] + "");
					if (obj[1] == null) {
						map.put("LAST_UPDATE_TIME", "");
					} else {
						map.put("LAST_UPDATE_TIME", obj[1] + "");
					}

					partnerObj.add(map);
				}

			}

		} catch (Exception e) {
			logger.info("NSP get Data Error! Exception :" + e);
			e.printStackTrace();
		}

		return partnerObj;

	}

	// 3
	public List getNspLastUpdateParterParamValue() {

		List<Map<String, String>> partnerObj = new ArrayList<Map<String, String>>();
		// Object[] partnerObj=null;
		try {

			SqlUtil bach = SqlUtil.newInstance();
			bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
					nsp_db_password);

			String sql_connect = "select PARTNER_PARAM_VALUE_ID,LAST_UPDATE_TIME from omc_partner_param_value where 1=1";

			List<Object[]> rst = bach.executeQuery(sql_connect, null);
			if (rst.size() > 0) {
				for (Object[] obj : rst) {
					Map<String, String> map = new HashMap<String, String>();

					map.put("PARTNER_PARAM_VALUE_ID", obj[0] + "");
					if (obj[1] == null) {
						map.put("LAST_UPDATE_TIME", "");
					} else {
						map.put("LAST_UPDATE_TIME", obj[1] + "");
					}

					partnerObj.add(map);
				}

			}

		} catch (Exception e) {
			logger.info("NSP get Data Error! Exception :" + e);
			e.printStackTrace();
		}

		return partnerObj;

	}

	// 4
	public List getNspLastUpdateInterface() {

		List<Map<String, String>> partnerObj = new ArrayList<Map<String, String>>();
		// Object[] partnerObj=null;
		try {

			SqlUtil bach = SqlUtil.newInstance();
			bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
					nsp_db_password);

			String sql_connect = "select INTERFACE_ID,LAST_UPDATE_TIME from omc_interface where 1=1";

			List<Object[]> rst = bach.executeQuery(sql_connect, null);
			if (rst.size() > 0) {
				for (Object[] obj : rst) {
					Map<String, String> map = new HashMap<String, String>();

					map.put("INTERFACE_ID", obj[0] + "");
					if (obj[1] == null) {
						map.put("LAST_UPDATE_TIME", "");
					} else {
						map.put("LAST_UPDATE_TIME", obj[1] + "");
					}

					partnerObj.add(map);
				}

			}

		} catch (Exception e) {
			logger.info("NSP get Data Error! Exception :" + e);
			e.printStackTrace();
		}

		return partnerObj;

	}

	// 5
	public List getNspLastUpdateInterfaceParam() {

		List<Map<String, String>> partnerObj = new ArrayList<Map<String, String>>();
		// Object[] partnerObj=null;
		try {

			SqlUtil bach = SqlUtil.newInstance();
			bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
					nsp_db_password);

			String sql_connect = "select INTERFACE_PARAM_ID,LAST_UPDATE_TIME from omc_interface_param where 1=1";

			List<Object[]> rst = bach.executeQuery(sql_connect, null);
			if (rst.size() > 0) {
				for (Object[] obj : rst) {
					Map<String, String> map = new HashMap<String, String>();

					map.put("INTERFACE_PARAM_ID", obj[0] + "");
					if (obj[1] == null) {
						map.put("LAST_UPDATE_TIME", "");
					} else {
						map.put("LAST_UPDATE_TIME", obj[1] + "");
					}

					partnerObj.add(map);
				}

			}

		} catch (Exception e) {
			logger.info("NSP get Data Error! Exception :" + e);
			e.printStackTrace();
		}

		return partnerObj;

	}

	// 6
	public List getNspLastUpdateInterfaceParamValue() {

		List<Map<String, String>> partnerObj = new ArrayList<Map<String, String>>();
		// Object[] partnerObj=null;
		try {

			SqlUtil bach = SqlUtil.newInstance();
			bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
					nsp_db_password);

			String sql_connect = "select PARAM_VALUE_ID,LAST_UPDATE_TIME from omc_interface_param_value where 1=1";

			List<Object[]> rst = bach.executeQuery(sql_connect, null);
			if (rst.size() > 0) {
				for (Object[] obj : rst) {
					Map<String, String> map = new HashMap<String, String>();

					map.put("PARAM_VALUE_ID", obj[0] + "");
					if (obj[1] == null) {
						map.put("LAST_UPDATE_TIME", "");
					} else {
						map.put("LAST_UPDATE_TIME", obj[1] + "");
					}

					partnerObj.add(map);
				}

			}

		} catch (Exception e) {
			logger.info("NSP get Data Error! Exception :" + e);
			e.printStackTrace();
		}

		return partnerObj;

	}
	
	/*
	 * 获取id和lastUpadatTime
	 * 传入对应的主键名称 主键值  表名 
	 * 
	 * 
	 * 
	 */
	public List getNspFkAndLastUpadte(String id,String from,String value) {

		List<Map<String, String>> partnerObj = new ArrayList<Map<String, String>>();
		// Object[] partnerObj=null;
		try {

			SqlUtil bach = SqlUtil.newInstance();
			bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
					nsp_db_password);

			String sql_connect = "select "+id+",LAST_UPDATE_TIME from "+from+" where 1=1 AND "+id+"='"+value+"'";

			List<Object[]> rst = bach.executeQuery(sql_connect, null);
			if (rst.size() > 0) {
				for (Object[] obj : rst) {
					Map<String, String> map = new HashMap<String, String>();

					map.put(id, obj[0] + "");
					if (obj[1] == null) {
						map.put("LAST_UPDATE_TIME", "");
					} else {
						map.put("LAST_UPDATE_TIME", obj[1] + "");
					}

					partnerObj.add(map);
				}

			}

		} catch (Exception e) {
			logger.info("NSP get Data Error! Exception :" + e);
			e.printStackTrace();
		}

		return partnerObj;

	}
	
	
	

	// omc_partner
	public void saveNspPartner(List<Map> list) throws ParseException {
		SqlUtil bach = SqlUtil.newInstance();
		bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
				nsp_db_password);
			// 对比数据
			for (Map omc : list) {
				List<Map> partnerParamValue = getNspFkAndLastUpadte("PARTNER_ID","omc_partner",omc.get("PARTNER_ID").toString());
				if(partnerParamValue.size()>0){
					//当omc的LastUpadteTime 为空时直接返回
					if(omc.get("LAST_UPDATE_TIME") == null){
						continue;
					}
				for (Map nsp : partnerParamValue) {

					String nspparid = nsp.get("PARTNER_ID").toString();
					String omcparid = omc.get("PARTNER_ID").toString();

					if (nspparid.equals(omcparid)) {
						if (!omc.get("LAST_UPDATE_TIME").equals(nsp.get("LAST_UPDATE_TIME"))) {
							String creattime = "";
							if (omc.get("CREATE_TIME") != null) {
								creattime = "'"
										+ omc.get("CREATE_TIME").toString()
										+ "'";
							} else {
								creattime = omc.get("CREATE_TIME") + "";
							}
							String lastupdattime = "";
							if (omc.get("LAST_UPDATE_TIME") != null) {
								lastupdattime = "'"
										+ omc.get("LAST_UPDATE_TIME")
												.toString() + "'";
							} else {
								lastupdattime = omc.get("LAST_UPDATE_TIME")
										+ "";
							}
							String sql = "update omc_partner set PARTNER_CODE='"
									+ omc.get("PARTNER_CODE")
									+ "',PARTNER_KEY='"
									+ omc.get("PARTNER_KEY")
									+ "',NAME='"
									+ omc.get("NAME")
									+ "',ADDRESS='"
									+ omc.get("ADDRESS")
									+ "',CONTACT_NAME='"
									+ omc.get("CONTACT_NAME")
									+ "',CONTACT_TEL='"
									+ omc.get("CONTACT_TEL")
									+ "',CONTACT_ADDRESS='"
									+ omc.get("CONTACT_ADDRESS")
									+ "',CONTACT_NOTE='"
									+ omc.get("CONTACT_NOTE")
									+ "',TECH_CONTACT_NAME='"
									+ omc.get("TECH_CONTACT_NAME")
									+ "',TECH_CONTACT_TEL='"
									+ omc.get("TECH_CONTACT_TEL")
									+ "',TECH_CONTACT_NOTE='"
									+ omc.get("TECH_CONTACT_NOTE")
									+ "',NOTE='"
									+ omc.get("NOTE")
									+ "',STATUS='"
									+ omc.get("STATUS")
									+ "',CREATE_USER="
									+ omc.get("CREATE_USER")
									+ ",CREATE_TIME="
									+ creattime
									+ ",LAST_UPDATE_USER="
									+ omc.get("LAST_UPDATE_USER")
									+ ",LAST_UPDATE_TIME="
									+ lastupdattime
									+ " where PARTNER_ID='"
									+ omc.get("PARTNER_ID") + "'";
							try {
								bach.executeUpdate(sql, null);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}

				}
				}else{
					String sql = "insert into omc_partner (PARTNER_ID,PARTNER_CODE,PARTNER_KEY,NAME,ADDRESS,CONTACT_NAME,CONTACT_TEL,CONTACT_ADDRESS,CONTACT_NOTE,TECH_CONTACT_NAME,TECH_CONTACT_TEL,TECH_CONTACT_NOTE,NOTE,STATUS,CREATE_USER,CREATE_TIME,LAST_UPDATE_USER,LAST_UPDATE_TIME) "
							+ "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

					List params = new ArrayList();
					params.add(0, omc.get("PARTNER_ID"));
					params.add(1, omc.get("PARTNER_CODE"));
					params.add(2, omc.get("PARTNER_KEY"));
					params.add(3, omc.get("NAME"));
					params.add(4, omc.get("ADDRESS"));
					params.add(5, omc.get("CONTACT_NAME"));
					params.add(6, omc.get("CONTACT_TEL"));
					params.add(7, omc.get("CONTACT_ADDRESS"));
					params.add(8, omc.get("CONTACT_NOTE"));

					params.add(9, omc.get("TECH_CONTACT_NAME"));
					params.add(10, omc.get("TECH_CONTACT_TEL"));
					params.add(11, omc.get("TECH_CONTACT_NOTE"));
					params.add(12, omc.get("NOTE"));
					params.add(13, omc.get("STATUS"));
					params.add(14, omc.get("CREATE_USER"));
					params.add(15, omc.get("CREATE_TIME"));
					params.add(16, omc.get("LAST_UPDATE_USER"));
					params.add(17, omc.get("LAST_UPDATE_TIME"));

					try {
						bach.executeUpdate(sql, params);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

			}

		
	}

	// omc_partner_interface
	public void saveNspPartnerInterface(List<Map> list) throws Exception {
		SqlUtil bach = SqlUtil.newInstance();
		bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
				nsp_db_password);
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		if (list.size() > 0) {
			for (Map omc : list) {
				List<Map> partnerInterface = getNspLastUpdatePartnerInterface(omc.get("PARTNER_INTERFACE_ID").toString());
				if(partnerInterface.size()>0){
					//当omc的LastUpadteTime 为空时直接返回
					if(omc.get("LAST_UPDATE_TIME") == null){
						continue;
					}
					for (Map parinter : partnerInterface) {
						String nspparid = parinter.get("PARTNER_INTERFACE_ID")
								.toString();
						String omcparid = omc.get("PARTNER_INTERFACE_ID")
								.toString();

						if (nspparid.equals(omcparid)) {
							if (!omc.get("LAST_UPDATE_TIME").equals(parinter.get("LAST_UPDATE_TIME"))) {
								String creattime = "";
								if (omc.get("CREATE_TIME") != null) {
									creattime = "'"
											+ omc.get("CREATE_TIME").toString()
											+ "'";
								} else {
									creattime = omc.get("CREATE_TIME") + "";
								}
								String lastupdattime = "";
								if (omc.get("LAST_UPDATE_TIME") != null) {
									lastupdattime = "'"
											+ omc.get("LAST_UPDATE_TIME")
													.toString() + "'";
								} else {
									lastupdattime = omc.get("LAST_UPDATE_TIME")
											+ "";
								}
								String sql = "update omc_partner_interface set "
										+ "PARTNER_ID="
										+ omc.get("PARTNER_ID")
										+ ""
										+ ",INTERFACE_ID="
										+ omc.get("INTERFACE_ID")
										+ ""
										+ ",STATUS='"
										+ omc.get("STATUS")
										+ "'"
										+ ",CREATE_USER="
										+ omc.get("CREATE_USER")
										+ ""
										+ ",CREATE_TIME="
										+ creattime
										+ ""
										+ ",LAST_UPDATE_USER="
										+ omc.get("LAST_UPDATE_USER")
										+ ""
										+ ",LAST_UPDATE_TIME="
										+ lastupdattime
										+ ""
										+ " where PARTNER_INTERFACE_ID='"
										+ omc.get("PARTNER_INTERFACE_ID") + "'";
								try {
									bach.executeUpdate(sql, null);
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}
						}
				}else {
						String sql = "insert into omc_partner_interface (PARTNER_INTERFACE_ID,PARTNER_ID,INTERFACE_ID,STATUS,CREATE_USER,CREATE_TIME,"
								+ "LAST_UPDATE_USER,LAST_UPDATE_TIME) "
								+ "values (?,?,?,?,?,?,?,?)";
						List params = new ArrayList();
						params.add(0, omc.get("PARTNER_INTERFACE_ID"));
						params.add(1, omc.get("PARTNER_ID"));
						params.add(2, omc.get("INTERFACE_ID"));
						params.add(3, omc.get("STATUS"));
						params.add(4, omc.get("CREATE_USER"));
						params.add(5, omc.get("CREATE_TIME"));
						params.add(6, omc.get("LAST_UPDATE_USER"));
						params.add(7, omc.get("LAST_UPDATE_TIME"));
						try {
							bach.executeUpdate(sql, params);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}
			}

	}

	// omc_partner_param_value
	public void saveNspPartnerParamValue(List<Map> list) throws Exception {
		SqlUtil bach = SqlUtil.newInstance();
		bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
				nsp_db_password);
		for (Map omc : list) {
			List<Map> partnerParamValue = getNspFkAndLastUpadte("PARTNER_PARAM_VALUE_ID","omc_partner_param_value",omc.get("PARTNER_PARAM_VALUE_ID").toString());
			if(partnerParamValue.size()>0){
				//当omc的LastUpadteTime 为空时直接返回
				if(omc.get("LAST_UPDATE_TIME") == null){
					continue;
				}
				for (Map parvalue : partnerParamValue) {
					
					String nspparid = parvalue.get("PARTNER_PARAM_VALUE_ID")
							.toString();
					String omcparid = omc.get("PARTNER_PARAM_VALUE_ID")
							.toString();
	
					if (nspparid.equals(omcparid)) {
							// || !omc.get("LAST_UPDATE_TIME").equals(parvalue.get("LAST_UPDATE_TIME"))				
						if (!omc.get("LAST_UPDATE_TIME").equals(parvalue.get("LAST_UPDATE_TIME"))) {
							String creattime = "";
							if (omc.get("CREATE_TIME") != null) {
								creattime = "'"
										+ omc.get("CREATE_TIME").toString()
										+ "'";
							} else {
								creattime = omc.get("CREATE_TIME") + "";
							}
							String lastupdattime = "";
							if (omc.get("LAST_UPDATE_TIME") != null) {
								lastupdattime = "'"
										+ omc.get("LAST_UPDATE_TIME")
												.toString() + "'";
							} else {
								lastupdattime = omc.get("LAST_UPDATE_TIME")
										+ "";
							}
							String sql = "update omc_partner_param_value set "
									+ "PARTNER_INTERFACE_ID="
									+ omc.get("PARTNER_INTERFACE_ID")
									+ ""
									+ ",INTERFACE_PARAM_ID="
									+ omc.get("INTERFACE_PARAM_ID")
									+ ""
									+ ",PARAM_VALUE_ID="
									+ omc.get("PARAM_VALUE_ID")
									+ ""
									+ ",PARAM_VALUE='"
									+ omc.get("PARAM_VALUE")
									+ "'"
									+ ",NOTE='"
									+ omc.get("NOTE")
									+ "'"
									+ ",STATUS='"
									+ omc.get("STATUS")
									+ "'"
									+ ",CREATE_USER="
									+ omc.get("CREATE_USER")
									+ ""
									+ ",CREATE_TIME="
									+ creattime
									+ ""
									+ ",LAST_UPDATE_USER="
									+ omc.get("LAST_UPDATE_USER")
									+ ""
									+ ",LAST_UPDATE_TIME="
									+ lastupdattime
									+ ""
									+ " where PARTNER_PARAM_VALUE_ID='"
									+ omc.get("PARTNER_PARAM_VALUE_ID") + "'";
							try {
								bach.executeUpdate(sql, null);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
			else {
				String sql = "insert into omc_partner_param_value (PARTNER_PARAM_VALUE_ID, PARTNER_INTERFACE_ID, INTERFACE_PARAM_ID, PARAM_VALUE_ID, PARAM_VALUE, NOTE,"
						+ "STATUS,CREATE_USER, CREATE_TIME,LAST_UPDATE_USER,LAST_UPDATE_TIME) "
						+ "values (?,?,?,?,?,?,?,?,?,?,?)";
				List params = new ArrayList();
				params.add(0, omc.get("PARTNER_PARAM_VALUE_ID"));
				params.add(1, omc.get("PARTNER_INTERFACE_ID"));
				params.add(2, omc.get("INTERFACE_PARAM_ID"));
				params.add(3, omc.get("PARAM_VALUE_ID"));
				params.add(4, omc.get("PARAM_VALUE"));
				params.add(5, omc.get("NOTE"));
				params.add(6, omc.get("STATUS"));
				params.add(7, omc.get("CREATE_USER"));
				params.add(8, omc.get("CREATE_TIME"));

				params.add(9, omc.get("LAST_UPDATE_USER"));
				params.add(10, omc.get("LAST_UPDATE_TIME"));
				try {
					bach.executeUpdate(sql, params);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			}

		
	}

	// omc_interface_param
	public void saveNspInterfaceParam(List<Map> list) throws Exception {
		SqlUtil bach = SqlUtil.newInstance();
		bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
				nsp_db_password);
		for (Map omc : list) {
			List<Map> partnerParamValue = getNspFkAndLastUpadte("INTERFACE_PARAM_ID","omc_interface_param",omc.get("INTERFACE_PARAM_ID").toString());
			if(partnerParamValue.size()>0){
				//当omc的LastUpadteTime 为空时直接返回
				if(omc.get("LAST_UPDATE_TIME") == null){
					continue;
				}
				for (Map parvalue : partnerParamValue) {
					String nspparid = parvalue.get("INTERFACE_PARAM_ID")
							.toString();
					String omcparid = omc.get("INTERFACE_PARAM_ID").toString();

					if (nspparid.equals(omcparid)) {
						if (!omc.get("LAST_UPDATE_TIME").equals(parvalue.get("LAST_UPDATE_TIME"))) {
							String creattime = "";
							if (omc.get("CREATE_TIME") != null) {
								creattime = "'"
										+ omc.get("CREATE_TIME").toString()
										+ "'";
							} else {
								creattime = omc.get("CREATE_TIME") + "";
							}
							String lastupdattime = "";
							if (omc.get("LAST_UPDATE_TIME") != null) {
								lastupdattime = "'"
										+ omc.get("LAST_UPDATE_TIME")
												.toString() + "'";
							} else {
								lastupdattime = omc.get("LAST_UPDATE_TIME")
										+ "";
							}
							String sql = "update omc_interface_param set "
									+ "INTERFACE_ID='"
									+ omc.get("INTERFACE_ID")
									+ "'"
									+ ",PARAM_KEY='"
									+ omc.get("PARAM_KEY")
									+ "'"
									+ ",PARAM_NAME='"
									+ omc.get("PARAM_NAME")
									+ "'"
									+ ",TYPE='"
									+ omc.get("TYPE")
									+ "'"
									+ ",NOTE='"
									+ omc.get("NOTE")
									+ "'"
									+ ",STATUS='"
									+ omc.get("STATUS")
									+ "'"
									+ ",CREATE_USER="
									+ omc.get("CREATE_USER")
									+ ""
									+ ",CREATE_TIME="
									+ creattime
									+ ""
									+ ",LAST_UPDATE_USER="
									+ omc.get("LAST_UPDATE_USER")
									+ ""
									+ ",LAST_UPDATE_TIME="
									+ lastupdattime
									+ ""
									+ " where INTERFACE_PARAM_ID='"
									+ omc.get("INTERFACE_PARAM_ID") + "'";
							try {
								bach.executeUpdate(sql, null);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
				
				
				
			}else{
					String sql = "insert into omc_interface_param (INTERFACE_PARAM_ID, INTERFACE_ID, PARAM_KEY, PARAM_NAME, TYPE, NOTE,"
							+ "STATUS,CREATE_USER, CREATE_TIME,LAST_UPDATE_USER,LAST_UPDATE_TIME) "
							+ "values (?,?,?,?,?,?,?,?,?,?,?)";
					List params = new ArrayList();
					params.add(0, omc.get("INTERFACE_PARAM_ID"));
					params.add(1, omc.get("INTERFACE_ID"));
					params.add(2, omc.get("PARAM_KEY"));
					params.add(3, omc.get("PARAM_NAME"));
					params.add(4, omc.get("TYPE"));
					params.add(5, omc.get("NOTE"));
					params.add(6, omc.get("STATUS"));
					params.add(7, omc.get("CREATE_USER"));
					params.add(8, omc.get("CREATE_TIME"));

					params.add(9, omc.get("LAST_UPDATE_USER"));
					params.add(10, omc.get("LAST_UPDATE_TIME"));
					try {
						bach.executeUpdate(sql, params);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

		
	}

	// omc_interface
	public void saveNspInterface(List<Map> list) throws Exception {
		SqlUtil bach = SqlUtil.newInstance();
		bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
				nsp_db_password);

			for (Map omc : list) {
				List<Map> partnerParamValue = getNspFkAndLastUpadte("INTERFACE_ID","omc_interface",omc.get("INTERFACE_ID").toString());
				if(partnerParamValue.size()>0){
					//当omc的LastUpadteTime 为空时直接返回
					if(omc.get("LAST_UPDATE_TIME") == null){
						continue;
					}
					for (Map parvalue : partnerParamValue) {
						String nspparid = parvalue.get("INTERFACE_ID").toString();
						String omcparid = omc.get("INTERFACE_ID").toString();

						if (nspparid.equals(omcparid)) {
							if (!omc.get("LAST_UPDATE_TIME").equals(parvalue.get("LAST_UPDATE_TIME"))) {
								String creattime = "";
								if (omc.get("CREATE_TIME") != null) {
									creattime = "'"
											+ omc.get("CREATE_TIME").toString()
											+ "'";
								} else {
									creattime = omc.get("CREATE_TIME") + "";
								}
								String lastupdattime = "";
								if (omc.get("LAST_UPDATE_TIME") != null) {
									lastupdattime = "'"
											+ omc.get("LAST_UPDATE_TIME")
													.toString() + "'";
								} else {
									lastupdattime = omc.get("LAST_UPDATE_TIME")
											+ "";
								}
								String sql = "update omc_interface set "
										+ "INTERFACE_CODE='"
										+ omc.get("INTERFACE_CODE")
										+ "'"
										+ ",URL='"
										+ omc.get("URL")
										+ "'"
										+ ",NAME='"
										+ omc.get("NAME")
										+ "'"
										+ ",TYPE='"
										+ omc.get("TYPE")
										+ "'"
										+ ",NOTE='"
										+ omc.get("NOTE")
										+ "'"
										+ ",STATUS='"
										+ omc.get("STATUS")
										+ "'"
										+ ",CREATE_USER="
										+ omc.get("CREATE_USER")
										+ ""
										+ ",CREATE_TIME="
										+ creattime
										+ ""
										+ ",LAST_UPDATE_USER="
										+ omc.get("LAST_UPDATE_USER")
										+ ""
										+ ",LAST_UPDATE_TIME="
										+ lastupdattime
										+ ""
										+ " where INTERFACE_ID='"
										+ omc.get("INTERFACE_ID") + "'";
								try {
									bach.executeUpdate(sql, null);
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}
					}
					
					
					
				}else{
					String sql = "insert into omc_interface (INTERFACE_ID, INTERFACE_CODE, URL, NAME, TYPE, NOTE,"
							+ "STATUS,CREATE_USER, CREATE_TIME,LAST_UPDATE_USER,LAST_UPDATE_TIME) "
							+ "values (?,?,?,?,?,?,?,?,?,?,?)";
					List params = new ArrayList();
					params.add(0, omc.get("INTERFACE_ID"));
					params.add(1, omc.get("INTERFACE_CODE"));
					params.add(2, omc.get("URL"));
					params.add(3, omc.get("NAME"));
					params.add(4, omc.get("TYPE"));
					params.add(5, omc.get("NOTE"));
					params.add(6, omc.get("STATUS"));
					params.add(7, omc.get("CREATE_USER"));
					params.add(8, omc.get("CREATE_TIME"));

					params.add(9, omc.get("LAST_UPDATE_USER"));
					params.add(10, omc.get("LAST_UPDATE_TIME"));
					try {
						bach.executeUpdate(sql, params);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					}
				}
			
	}

	// omc_interface_param_value
	public void saveNspInterfaceParamValue(List<Map> list) throws Exception {
		SqlUtil bach = SqlUtil.newInstance();
		bach.setConnection(nsp_db_driver, nsp_db_url, nsp_db_username,
				nsp_db_password);
		for (Map omc : list) {
			List<Map> partnerParamValue = getNspFkAndLastUpadte("PARAM_VALUE_ID","omc_interface_param_value",omc.get("PARAM_VALUE_ID").toString());
			if(partnerParamValue.size()>0){
			
				for (Map parvalue : partnerParamValue) {
					String nspparid = parvalue.get("PARAM_VALUE_ID").toString();
					String omcparid = omc.get("PARAM_VALUE_ID").toString();

					if (nspparid.equals(omcparid)) {
						if (!omc.get("LAST_UPDATE_TIME").equals(parvalue.get("LAST_UPDATE_TIME"))) {
							String creattime = "";
							if (omc.get("CREATE_TIME") != null) {
								creattime = "'"
										+ omc.get("CREATE_TIME").toString()
										+ "'";
							} else {
								creattime = omc.get("CREATE_TIME") + "";
							}
							String lastupdattime = "";
							if (omc.get("LAST_UPDATE_TIME") != null) {
								lastupdattime = "'"
										+ omc.get("LAST_UPDATE_TIME")
												.toString() + "'";
							} else {
								lastupdattime = omc.get("LAST_UPDATE_TIME")
										+ "";
							}
							String sql = "update omc_interface_param_value set "
									+ "INTERFACE_PARAM_ID="
									+ omc.get("INTERFACE_PARAM_ID")
									+ ""
									+ ",PARAM_VALUE_DIAPLAY='"
									+ omc.get("PARAM_VALUE_DIAPLAY")
									+ "'"
									+ ",PARAM_VALUE='"
									+ omc.get("PARAM_VALUE")
									+ "'"
									+ ",NOTE='"
									+ omc.get("NOTE")
									+ "'"
									+ ",STATUS='"
									+ omc.get("STATUS")
									+ "'"
									+ ",CREATE_USER="
									+ omc.get("CREATE_USER")
									+ ""
									+ ",CREATE_TIME="
									+ creattime
									+ ""
									+ ",LAST_UPDATE_USER="
									+ omc.get("LAST_UPDATE_USER")
									+ ""
									+ ",LAST_UPDATE_TIME="
									+ lastupdattime
									+ ""
									+ " where PARAM_VALUE_ID='"
									+ omc.get("PARAM_VALUE_ID") + "'";
							try {
								bach.executeUpdate(sql, null);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
				
				
			}else{
				String sql = "insert into omc_interface_param_value (PARAM_VALUE_ID, INTERFACE_PARAM_ID, PARAM_VALUE_DIAPLAY, PARAM_VALUE, NOTE, STATUS,"
						+ "CREATE_USER,CREATE_TIME, LAST_UPDATE_USER,LAST_UPDATE_TIME) "
						+ "values (?,?,?,?,?,?,?,?,?,?)";
				List params = new ArrayList();
				params.add(0, omc.get("PARAM_VALUE_ID"));
				params.add(1, omc.get("INTERFACE_PARAM_ID"));
				params.add(2, omc.get("PARAM_VALUE_DIAPLAY"));
				params.add(3, omc.get("PARAM_VALUE"));
				params.add(4, omc.get("NOTE"));
				params.add(5, omc.get("STATUS"));
				params.add(6, omc.get("CREATE_USER"));
				params.add(7, omc.get("CREATE_TIME"));

				params.add(8, omc.get("LAST_UPDATE_USER"));
				params.add(9, omc.get("LAST_UPDATE_TIME"));

				try {
					bach.executeUpdate(sql, params);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		RemoteNspDAO remoteNspDAO = new RemoteNspDAO();
		remoteNspDAO.getNspLogger("5000");
		remoteNspDAO.getNspTellog("3000");

	}

}
